// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    userInfo:'',
    phone:'',
    wx_nickname:'',
    wx_avatar:'',
  },
  onExit(){
    app.globalData.is_finsih_wx=''//是否传用户信息
    app.globalData.is_active=''//是否需要激活
    app.globalData.wx_nickname=''//匿名
    app.globalData.wx_avatar=''//头像
    app.globalData.phone=''//手机号
    app.globalData.name = '';
    app.globalData.card=''
    wx.clearStorage({
      success(res){
          wx.redirectTo({
        url: '/pages/login/index',
      })
      }
    }); 
  },
  onShow() {
    if(app.globalData.wx_nickname && app.globalData.wx_avatar){
      this.setData({
        phone:app.globalData.phone,
        wx_nickname:app.globalData.wx_nickname,
        wx_avatar:app.globalData.wx_avatar
      })
    }else {
      this.onGetUserInfo()
    }
    
  },
  onGetUserInfo(){
    let that =this;
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/my/userInfo',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token':app.globalData.token,
      },
      success: function(res) {  
        wx.hideLoading()    
         
        if(res.data.status){
          if(!res.data.data.records.is_real){
            that.onExit();
            return false;
          } 
          app.globalData.is_finish_wx = res.data.data.records.is_finish_wx;
          app.globalData.wx_avatar = res.data.data.records.wx_avatar;
          app.globalData.wx_nickname = res.data.data.records.wx_nickname;
     
          app.globalData.phone = res.data.data.records.phone;
          app.globalData.card = res.data.data.records.get_driver_info?res.data.data.records.get_driver_info.id_card:'';
          app.globalData.name = res.data.data.records.get_driver_info?res.data.data.records.get_driver_info.name:'';
          app.globalData.is_active = res.data.data.records.is_active
          that.setData({
            is_active: res.data.data.records.is_active
          })
          
        }
      },
      fail: function() {
        wx.hideLoading()    
        console.log("请求失败")
      }
    })
  },
  onGetWxInfo(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否授权获取用户信息？',
      success (res) {
        if (res.confirm) {
          that.getUserProfile()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onGetCocact(){
    if(app.globalData.concat){
      wx.showModal({
        title: '联系平台',
        content: app.globalData.concat,
        success (res) {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: app.globalData.concat //仅为示例，并非真实的电话号码
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false
    }
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/setting/lst',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function(res) {  
        wx.hideLoading()    
        if(res.data.status){
          app.globalData.concat = res.data.data.records[0].val ;
          wx.showModal({
            title: '联系平台',
            content: app.globalData.concat,
            success (ras) {
              if (ras.confirm) {
                wx.makePhoneCall({
                  phoneNumber: res.data.data.records[0].val //仅为示例，并非真实的电话号码
                })
              } else if (ras.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
        
      },
      fail: function() {
        wx.hideLoading()    
        console.log("请求失败")
      }
    })

  }
  ,
  getUserProfile(e) {
		var that = this;
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
				console.log(res)
          that.setData({
            wx_nickname: res.userInfo.nickName,
            wx_avatar: res.userInfo.avatarUrl,
            phone:res.userInfo.phone
          })
      }
    })
  },
})
